/*
 * Copyright (C) 2013 'Chaos'.Personal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.silly.adapter.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * @author Chaos	
 * @Description TODO
 * @date 2013-4-9 
 */
public class RUtils {
    
    public static void jump(Activity activity, Class<? extends Activity> clazz){
        jump(activity, clazz, true);
    }

    public static void jump(Activity activity, Class<? extends Activity> clazz, boolean canBack) {
        jump(activity, clazz, canBack, null, null);
    }

    public static void jump(Activity activity, Class<? extends Activity> clazz, boolean canBack,
            String[] key, Serializable[] value) {
        Intent intent = new Intent();
        intent.setClass(activity.getApplicationContext(), clazz);

        if (key != null && value != null) {
            int length = Math.min(key.length, value.length);

            for (int i = 0; i < length; i++) {
                intent.putExtra(key[i], value[i]);
            }
        }

        if (!canBack) {
            activity.finish();
        }

        activity.startActivity(intent);
    }
    
    public static void showTip(Context context, String tip, boolean isLong) {
        Toast.makeText(context, tip, isLong?Toast.LENGTH_LONG:Toast.LENGTH_SHORT).show();
    }
    
    public static void showTip(Context context, String tip){
        showTip(context, tip, false);
    }
    
    public static Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    public static byte[] ObjectToByte(java.lang.Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
    }
    
}
